Page({
  data: {
    hasData: false,
    evaluations: []
  },
  onLoad() {
    this.loadData();
  },
  loadData() {
    this.fetchDataFromServer();
  },
  fetchDataFromServer() {
    const app = getApp();
    wx.request({
      url: 'http://localhost:8080/hqy/record/user', // 替换为你的服务器地址
      method: 'GET',
      data: {
        userId: app.globalData.user.id // 从全局变量中获取 userId
      },
      header: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + app.globalData.user.token // 在请求头中
      },
      
      success: (res) => {
        console.log('cep',res);
        if (res.statusCode === 200 && res.data.code === 200) {
          // 假设后端返回的数据结构是 {code: 200, data: [...]}
          const records = res.data.data || [];
          // 如果需要格式化时间，可以在这里进行处理
          const formattedRecords = records.map(record => {
            return {
              ...record,
              consultationTime: this.formatDate(record.consultationTime)
            };
          });
          this.setData({
            evaluations: formattedRecords,
            hasData: formattedRecords.length > 0
          });
          // 同时将数据保存到全局变量中
          app.globalData.mockEvaluations = formattedRecords;
        } else {
          // 请求失败，处理错误
          console.error('获取数据失败:', res);
          this.setData({
            evaluations: [],
            hasData: false
          });
          wx.showToast({
            title: '获取数据失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        // 请求失败，处理错误
        console.error('请求失败:', err);
        this.setData({
          evaluations: [],
          hasData: false
        });
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },
  formatDate(dateString) {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
});